package com.fdl.action.test;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.fdl.action.support.SupportAction;
import com.fdl.common.utils.Constants;
import com.fdl.dao.base.impl.Page;
import com.fdl.service.dto.TestDTO;
import com.fdl.service.test.TestService;

/**
 * @description
 * @project: hb-intra
 * @Date:2010-8-5
 * @version 1.0
 * @Company: 33e9
 * @author jing.qiu
 */
public class PageAction extends SupportAction{
	
	/**
	 * 获取日志对象
	 */
	private Log log = LogFactory.getLog(PageAction.class);
	
	/**
	 * 实体对象中ID
	 */
	private Long id;

	/**
	 * 用于保存查询条件
	 */
	private String value;
	
	/**
	 * Test业务接口
	 */
	private TestService testService;
	
	/**
	 * 实体DTO对象
	 */
	private TestDTO testDTO = null;
	
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	public void setTestService(TestService testService) {
		this.testService = testService;
	}
	public TestDTO getTestDTO() {
		return testDTO;
	}
	public void setTestDTO(TestDTO testDTO) {
		this.testDTO = testDTO;
	}

	
	/**
	 * 根据条件查询记录进行分页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findPage() throws Exception {
		
		HttpServletRequest request=ServletActionContext.getRequest();
		
		pageno=getPageNoPara(request,totalPageCount,pageno);
		
		isQueryOperateType();
		
		Page<TestDTO> page = testService.findTestByValue(value, pageno, pageSize);
		
		wrapPageInfo(page);
		
		if(messageMap.containsKey("message")){
			request.setAttribute("message",messageMap.get("message"));
			messageMap.clear();
		}

		request.setAttribute("page", page);
		return "listPage";
	}
	
	/**
	 * 删除 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception{
		try {
			testService.removeTest(id);
			messageMap.put("message", Constants.DELETE_SUCCESS);
			pageno=getPageNoParaByLastCount(totalCount,pageSize,pageno);
		} catch (Exception e) {
			messageMap.put("message", Constants.DELETE_ERROR);
			log.info("remove test has happen exception!", e);
		}
		return "listPage";
	}
	
	/**
	 * 更新前载入对象的方法
	 * @return
	 * @throws Exception
	 */
	public String updatePage()throws Exception{
		try {
			testDTO=testService.load(id);
		} catch (Exception e) {
			log.info("update test has happen exception!", e);
		}
		return "updatePage";
	}
	
	/**
	 * 更新的方法
	 * TODO
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception{
		try {
			testService.updateTest(testDTO);
			messageMap.put("message", Constants.UPDATE_SUCCESS);
			if(null!=value&&!"".equals(value)){
				pageno=getPageNoParaByLastCount(totalCount,pageSize,pageno);
			}
		} catch (Exception e) {
			messageMap.put("message", Constants.UPDATE_ERROR);
			log.info("update test has happen exception!", e);
		}
		return "listPage";
	}
	
	/**
	 * 新增前跳转页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addPage() throws Exception {
		this.setDTO2Null();
		return "addPage";
	}
	
	/**
	 * 新增
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		try {
			testService.addTest(testDTO);
			messageMap.put("message", Constants.SAVE_SUCCESS);
			value="";
			pageno=1;
		} catch (Exception e) {
			messageMap.put("message", Constants.SAVE_ERROR);
			log.info("add test has happen exception!", e);
		} finally {
			testDTO = null;
		}
		return "listPage";
	}
	
	/**
	 * 清除testDTO内容
	 */
	private void setDTO2Null() {
		testDTO = null;
	}
	
	/**
	 * 操作类型是查询操作
	 */
	private void isQueryOperateType(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String operateType=request.getParameter("operateType")==null?"":request.getParameter("operateType");
		if("query".equals(operateType)){
			pageno=1;
			operateType="";
		}
	}
}
